# This file is part of Neuroinfo Toolkit.
#
# Neuroinfo Toolkit is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Neuroinfo Toolkit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Neuroinfo Toolkit.  If not, see <http://www.gnu.org/licenses/>.

import unittest
from neuro.filesystem import *
from neuro.exceptions import IllegalArgumentException
from neuro.apps.xnat import *
from neuro.net.transport import TransportException

class TestXnat(unittest.TestCase):
	'''
	Test XNAT
	'''
	def setUp(self):
		self._xnat = Xnat("gspcentral")
		self._testData = "../../../../test_data.txt"

	def test_instance(self):
		self.assertRaises(TypeError, Xnat)
		self.assertRaises(ConfigException, Xnat, "missing")
		self.assertRaises(IllegalArgumentException, Xnat, 1)

	def test_loadConfig(self):
		# --- no arguments
		self.assertRaises(TypeError, self._xnat.loadConfig)

		## --- bad first argument
		self.assertRaises(IllegalArgumentException, self._xnat.loadConfig, deployment="")
		self.assertRaises(IllegalArgumentException, self._xnat.loadConfig, deployment=1)

		## --- bad second argument
		self.assertRaises(IllegalArgumentException, self._xnat.loadConfig, deployment="test1", filename="")
		self.assertRaises(FileNotFoundException, self._xnat.loadConfig, deployment="test1", filename="./missing.xml")

		## --- missing deployment
		self.assertRaises(ConfigException, self._xnat.loadConfig, deployment="missing", filename="./config.xml")

		## --- no URL
		self.assertRaises(ConfigException, self._xnat.loadConfig, deployment="test2", filename="./config.xml")

		## --- no username
		self.assertRaises(ConfigException, self._xnat.loadConfig, deployment="test3", filename="./config.xml")

		## --- no password
		self.assertRaises(ConfigException, self._xnat.loadConfig, deployment="test4", filename="./config.xml")

		## --- load OK
		assert self._xnat.loadConfig(deployment="test1", filename="./config.xml") is None

	def test_getURL(self):
		## --- load a test config
		self._xnat.loadConfig(deployment="test1", filename="./config.xml")

		self.assertEqual(self._xnat.getURL(), "http://localhost:4321")

	def test_getUsername(self):
		## --- load a test config
		self._xnat.loadConfig(deployment="test1", filename="./config.xml")
		
		self.assertEqual(self._xnat.getUsername(), "foo")
		
	def test_getPassword(self):
		## --- load a test config
		self._xnat.loadConfig(deployment="test1", filename="./config.xml")
		
		self.assertEqual(self._xnat.getPassword(), "bar")

	def test_getDeployment(self):
		## --- load a test config
		self._xnat.loadConfig(deployment="test1", filename="./config.xml")

		self.assertEqual(self._xnat.getDeployment(), "test1")
		
	def test_connect(self):
		## --- this should work as both the config file exists and
		self._xnat.loadConfig(deployment="test1", filename="./config.xml")

		## --- this should fail as there should be nothing listening on http://localhost:4321
		self.assertRaises(TransportException, self._xnat.connect)

	def test_getOverview(self):
		## --- bad format argument
		self.assertRaises(IllegalArgumentException, self._xnat.getOverview, format="")
		self.assertRaises(IllegalArgumentException, self._xnat.getOverview, format="foo")

		## --- bad version
		self.assertRaises(OverviewException, self._xnat.getOverview, format="list", version="foo")

		## --- good version, should be a dict
		ov = self._xnat.getOverview(format="dict", version="overview")
		self.assertTrue(isinstance(ov, dict))

	def test_overview(self):
		## --- good version, should be a dict
		ov = self._xnat.getOverview(format="dict", version="overview")
		self.assertTrue(isinstance(ov, dict))

		## --- request test_data.txt
		content = fileGetContents(self._testData)
		testData = strings.toArray(content, row="\n", col=",")

		for row in testData:
			id = row[0]
			tag = row[1]
			value = row[2]
			self.assertEqual(ov[id][tag], value)

unittest.main()